package com.pesilo.mvc.view.component.show.watch
{
	import com.greensock.TweenMax;
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.ImageLoader;
	import com.pesilo.base.BaseComponent;
	import com.pesilo.event.PesiloEvent;
	import com.pesilo.mvc.locator.ModelLocator;
	import com.pesilo.util.Logger;
	import com.pesilo.util.UIUtil;
	import com.pesilo.watch.ui.WatchItemPanel;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	public class WatchItemComponent extends BaseComponent
	{
		private var index:int;
		private var img:Sprite;
		private var url:String;
		
		private var watchItem:WatchItemPanel;
		private var imagePanel:Sprite;
		private var inverted:Sprite;
		private var invertedMask:Sprite;
		
		private var inverBMD:BitmapData;
		private var inverBMP:Bitmap;
		
		private var progressUI:Sprite;
		private var progress:Sprite;
		private var progressBg:Sprite;
		
		private var loadedPanel:Sprite;
		private var loadedMask:Sprite;
		
		private var imageLoader:ImageLoader;
		
		public function WatchItemComponent(index:int,obj:*,url:String)
		{
			this.index=index;
			this.img=obj as Sprite;
			this.url=url;
			super();
		}
		
		override public function initUI():void{
			watchItem=new WatchItemPanel();
			this.addChild(watchItem);
			imagePanel=UIUtil.getElement(watchItem,"imagePanel")as Sprite;
			inverted=UIUtil.getElement(watchItem,"inverted")as Sprite;
			invertedMask=UIUtil.getElement(watchItem,"invertedMask")as Sprite;
			
			imagePanel.addChild(img);
			
			inverBMD=new BitmapData(inverted.width,inverted.height);
			inverBMD.draw(img);
			inverBMP=new Bitmap(inverBMD,"auto",true);
			inverBMP.scaleY=-1;
			inverBMP.y=inverBMP.height;
			
			inverted.addChild(inverBMP);
			
			inverted.cacheAsBitmap=true;
			invertedMask.cacheAsBitmap=true;
			inverted.mask=invertedMask;
			
			////loading bit image
			progressUI=UIUtil.getElement(watchItem,"progressUI")as Sprite;
			progress=UIUtil.getElement(progressUI,"progress")as Sprite;
			progressBg=UIUtil.getElement(progressUI,"progressBg")as Sprite;
			progressUI.visible=false;
			
			loadedPanel=UIUtil.getElement(watchItem,"loadedPanel")as Sprite;
			loadedMask=UIUtil.getElement(watchItem,"loadedMask")as Sprite;
			loadedPanel.mask=loadedMask;
			loadedPanel.cacheAsBitmap=true;
			loadedMask.cacheAsBitmap=true;
			
		}
		
		private function LoadingImage():void{
			progress.width=0;
			progressUI.visible=true;
			progressUI.alpha=0;
			TweenMax.to(progressUI,0.2,{alpha:1});
			
			loadedMask.alpha=1;
			
			imageLoader=new ImageLoader(url,{
				name:"imgLoader",
				onProgress:__progressHandler,
				onComplete:__completeHandler, 
				onError:__errorHandler,
				container:loadedPanel,
				centerRegistration:true,
				width:stage.stageWidth,
				height:stage.stageHeight,
				scaleMode:"proportionalOutside",
				hAlign:"center"
			});
			imageLoader.load();
		}
		
		private function __progressHandler(e:LoaderEvent):void{
			progress.width=e.target.progress*progressBg.width;
		}
		
		private function __completeHandler(e:LoaderEvent):void{
			TweenMax.to(progressUI,0.2,{alpha:0});
			TweenMax.to(loadedMask,0.5,{delay:0.2,width:420,height:280});
			TweenMax.to(loadedMask,0.5,{delay:0.8,width:stage.stageWidth,height:stage.stageHeight+100});
		}
		
		private function __errorHandler(e:LoaderEvent):void{
			Logger.logInfo("load bit watch item error");
		}
		
		public function current():void{
			if(ModelLocator.watchCount["current"]){
				ModelLocator.watchCount["current"].unloadImage();
			}
			if(ModelLocator.watchCount["current"]!=this){
				ModelLocator.watchCount["current"]=this;
			}
		}
		
		override public function initEvent(b:Boolean=true):void{
			if(b){
				img.addEventListener(MouseEvent.CLICK,__imgClickHandle);
				loadedPanel.addEventListener(MouseEvent.CLICK,__loadedPanelClickHandle);
			}else{
				img.removeEventListener(MouseEvent.CLICK,__imgClickHandle);
				loadedPanel.removeEventListener(MouseEvent.CLICK,__loadedPanelClickHandle);
			}
		}
		
		protected function __loadedPanelClickHandle(e:MouseEvent):void
		{
			unloadImage();
		}
		
		/**
		 * 卸载加载
		 */
		private function unloadImage():void
		{
			if(imageLoader){
				imageLoader.pause();
			}
			TweenMax.to(progressUI,0.2,{alpha:0});
			TweenMax.to(loadedMask,0.5,{width:10,height:2,alpha:0,onComplete:__unloadComplete});
		}
		
		private function __unloadComplete():void{
			if(imageLoader){
				imageLoader.unload();
				imageLoader.dispose(true);
				imageLoader=null;
			}
		}
		
		protected function __imgClickHandle(e:MouseEvent):void
		{
			if(ModelLocator.watchCount["current"]==this){
				trace("target click "+url);
				LoadingImage();
			}else{
				dispatchEvent(new PesiloEvent(PesiloEvent.ITEM_CLICK_EVENT,index));
			}
		}
		
		override public function initLocation():void{
			watchItem.x=-imagePanel.width/2;
			watchItem.y=-imagePanel.height/2;
			
//			unloadImage();
		}
	}
}